/*
 * Copyright 2008 to the author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lucifer.scene.importers;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.lucifer.scene.importers.util.RawCameraRepresentation;
import org.lucifer.scene.importers.util.RawSceneRepresentation;

/**
 * This class writes an lcf file based on raw scene data.
 * 
 * @author Mario Gonzalez
 * @author Henrik Engstroem
 */
public class LCFFileWriter {
	// Constants for xml generation
	private static final String cHEADER = 
		"<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	private static final String cLUCIFER_SCENE_BEGIN = "<luciferscene>";
	private static final String cLUCIFER_SCENE_END = "</luciferscene>";
	private static final String cCAMERA_BEGIN = "<camera>";
	private static final String cCAMERA_END = "</camera>";
	private static final String cLOCATION_BEGIN = "<location>";
	private static final String cLOCATION_END = "</location>";
	private static final String cTARGET_BEGIN = "<target>";
	private static final String cTARGET_END = "</target>";
	private static final String cMATERIAL_LIST_BEGIN = "<materiallist>";
	private static final String cMATERIAL_LIST_END = "</materiallist>";
	private static final String cMATERIAL_BEGIN = "<material>";
	private static final String cMATERIAL_END = "</material>";
	private static final String cCOLOR_BEGIN = "<color>";
	private static final String cCOLOR_END = "</color>";
	private static final String cCOLOR_R_BEGIN = "</r>";
	private static final String cCOLOR_R_END = "</r>";
	private static final String cCOLOR_G_BEGIN = "<g>";
	private static final String cCOLOR_G_END = "</g>";
	private static final String cCOLOR_B_BEGIN = "<b>";
	private static final String cCOLOR_B_END = "</b>";
	
	// Members
	private FileWriter _fileWriter;
	private BufferedWriter _bufferedWriter;
	
	/**
	 * Class constructor
	 */
	public LCFFileWriter() {
		
	}
	
	/**
	 * Writes a raw scene to a file in the lcf file format.
	 * 
	 * @param aFileName the file name and path of the file to write
	 * @param aScene the scene to persist
	 */
	public void writeFile(String aFileName, RawSceneRepresentation aScene) {
		try {
			// Initialize writers
			_fileWriter = new FileWriter(aFileName);
			_bufferedWriter = new BufferedWriter(_fileWriter, 64);
			
			// Write the xml header
			writeHeader();
			
			// Write the scene
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Writes the xml header
	 * @throws IOException
	 */
	private void writeHeader() throws IOException {
		// Write the header
		_bufferedWriter.write(cHEADER);
		_bufferedWriter.newLine();
	}
	
	/**
	 * Writes the scene data to the file and delegates to helper methods as 
	 * needed
	 * 
	 * @param aScene the scene data
	 * @throws IOException
	 */
	private void writeScene(RawSceneRepresentation aScene) throws IOException {
		_bufferedWriter.write(cLUCIFER_SCENE_BEGIN);
		_bufferedWriter.newLine();
		_bufferedWriter.write(cLUCIFER_SCENE_END);
	}
}
